package com.example.reflectdemo.classloader;

public class ClassLoaderTest {

    static class C1{
        static {
            System.out.println("C1");
        }
    }

    static class C2{
        static {
            System.out.println("C2");
        }
    }

    static class C3{
        static {
            System.out.println("C3");
        }
    }

    public static void main(String... args) throws Exception{
        System.out.println("new start");
        // 构造函数为类的静态引用，触发类型加载
        new C1();
        new C1();
        System.out.println("new end");

        System.out.println();

        System.out.println("Class.forName start");
        // Class.forName为Class上的静态函数，用于强制加载Class
        Class.forName("com.example.reflectdemo.classloader.ClassLoaderTest$C2");
        Class.forName("com.example.reflectdemo.classloader.ClassLoaderTest$C2");
        System.out.println("Class.forName end");

        System.out.println();

        System.out.println("C3.class start");
        // Class引用，会触发Class加载，但是不会触发初始化
        Class c1 = C3.class;
        Class c2 = C3.class;
        System.out.println("C3.class end");

        System.out.println();

        System.out.println("c1.newInstance start");
        // 调用class上的方法，触发初始化逻辑
        c1.newInstance();
        System.out.println("c1.newInstance end");

    }
}
